﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ServiceManager.Utils;
using ServiceStack.Redis;

namespace QuotationService.RealtimeQuotation
{
    /// <summary>
    /// 当日日线通道
    /// </summary>
    [DebuggerNonUserCode]
    internal static class DailyDataChannel
    {
        /// <summary>
        /// 保存当日日线信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool Update(QuotationSource source, string code, byte[] data)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            if (data == null || data.Length == 0)
                throw new ArgumentNullException("data");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            //保存
            return publisher.Update(source.ToFormatString() + ":" + QuotationDataType.DailyData.ToFormatString(), code, data);
        }

        /// <summary>
        /// 获取所有商品的最新日线数据
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<byte[]> GetAll(QuotationSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            using (var conn = QuotationSubscribeServiceContainer.GetNeedDisposedConnection(source))
            {
                var retValues = (conn as RedisClient).HVals(source.ToFormatString() + ":" + QuotationDataType.DailyData.ToFormatString());
                if (retValues == null)
                    return null;

                return retValues.ToList();
            }
        }

        /// <summary>
        /// 获取单只商品的最新分线数据
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static byte[] Get(QuotationSource source, string code)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            using (var conn = QuotationSubscribeServiceContainer.GetNeedDisposedConnection(source))
            {
                return (conn as RedisClient).HGet(source.ToFormatString() + ":" + QuotationDataType.DailyData.ToFormatString(),
                                code.ToUtf8Bytes());
            }
        }

        /// <summary>
        /// 删除单只商品的行情信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool Clear(QuotationSource source, string code)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("code");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            return publisher.Delete(source.ToFormatString() + ":" + QuotationDataType.DailyData.ToFormatString(), code);
        }

        /// <summary>
        /// 删除所有商品的日线数据
        /// </summary>
        /// <param name="source"></param>
        public static bool ClearAll(QuotationSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            return publisher.Delete(source.ToFormatString() + ":" + QuotationDataType.DailyData.ToFormatString());
        }
    }

}
